package com.test.map.HTTP;

import android.util.Log;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HTTP {
    private String urlAddress = "http://cloud.bmob.cn/0906a62b462a3082/";//服务器地址
    private String method = "getMemberBySex";//方法名
    private String getUrl;

    void get(String s) {
        getUrl = urlAddress + method + "?sex" + s;//构造getUrl
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(getUrl);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();//开启连接
                    connection.connect();//连接服务器
                    if (connection.getResponseCode() == 200) {
                        //使用字符流形式进行回复
                        InputStream is = connection.getInputStream();
                        //读取信息BufferReader
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                        StringBuffer buffer = new StringBuffer();
                        String readLine = "";
                        while ((readLine = reader.readLine()) != null) {
                            buffer.append(readLine);
                        }
                        is.close();
                        reader.close();
                        connection.disconnect();
                        Log.d("Msg", buffer.toString());
                    } else {
                        Log.d("TAG", "ERROR CONNECTED");
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    void post(final String s) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(urlAddress + method);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    //设置输入输出流
                    connection.setDoOutput(true);
                    connection.setDoInput(true);
                    connection.setUseCaches(false);
                    connection.setRequestMethod("POST");//设置为POST方法
                    //开始设置请求头
                    connection.setRequestProperty("Accept-Charset", "UTF-8");
                    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    connection.connect();//开始连接
                    //以输出流的形式进行给服务器传输数据
                    DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
                    String content = "sex=" + s;
                    outputStream.writeBytes(content);
                    outputStream.flush();
                    outputStream.close();
                    //服务器进行响应
                    if (connection.getResponseCode() == 200) {
                        InputStream is = connection.getInputStream();
                        //读取信息
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                        StringBuffer stringBuffer = new StringBuffer();//使用StringBuffer来存储所有信息
                        String readLine = "";//使用readLine方法来存储整行信息
                        while ((readLine = reader.readLine()) != null) {
                            stringBuffer.append(readLine);
                        }
                        is.close();
                        reader.close();
                        connection.disconnect();
                        Log.d("Message", stringBuffer.toString());
                    } else {
                        //打印错误的信息
                        Log.d("TAG", "ERROR ");
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
